﻿using System;
using CNav.Business.Services;
using CNav.Domain.Admin;
using CNav.Support;
using System.Linq;
using System.Web.Mvc;
using WebGrease.Css.Extensions;

namespace CNav.Controllers
{
    [Authorize]
    [AdminAuthorize]
    public class FormDesignController : BaseController
    {
        /// <summary>
        /// Display all input controls for selected Form id
        /// </summary>
        /// <param name="formId">form id</param>
        public ActionResult Index(int formId = 0)
        {
            if (formId == 0)
                return RedirectToRoute("Home");

            var formdetailList = Service.Get<IUiService>().GetCustomFormList(formId);

            Session[Constant.FormId] = formId;
            return View(formdetailList.Data.OrderBy(f => f.DisplayOrder).ToList());
        }

        public ActionResult Details(int id = 0)
        {
            var response = Service.Get<IUiService>().GetCustomFormById(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }

            return View(response.Data);
        }

        public ActionResult Create()
        {
            if (Session[Constant.FormId] == null)
                return RedirectToRoute("Home");

            int formId = (int)Session[Constant.FormId];

            var formDesign = new FormDesign();
            var response = Service.Get<UiService>().GetFormById(formId);
            var formData = response.Data;
            formDesign.FormName = formData.Name;
            formDesign.FormID = formData.ID;

            PopulateUiControls(formDesign);
            return View(formDesign);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(FormDesign formdetail)
        {
            if (ModelState.IsValid)
            {
                Service.Get<UiService>().AddCustomForm(formdetail);
                Session[Constant.FormId] = formdetail.FormID;
                return RedirectToAction("Index", new { formId = formdetail.FormID });
            }

            if (Session[Constant.FormId] == null)
                return RedirectToRoute("Home");

            int formId = (int)Session[Constant.FormId];

            var response = Service.Get<UiService>().GetFormById(formId);
            var formData = response.Data;
            formdetail.FormName = formData.Name;

            PopulateUiControls(formdetail);
            return View(formdetail);
        }

        public ActionResult Edit(int id = 0)
        {
            var response = Service.Get<IUiService>().GetCustomFormById(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }

            PopulateUiControls(response.Data);
            return View(response.Data);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(FormDesign formdetail)
        {
            Session[Constant.FormId] = formdetail.FormID;

            if (ModelState.IsValid)
            {
                var response = Service.Get<IUiService>().UpdateCustomForm(formdetail);
                if (response.Error == null)
                    return RedirectToAction("Index", new { formId = formdetail.FormID });

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            PopulateUiControls(formdetail);
            return View(formdetail);
        }

        public ActionResult Delete(int id = 0)
        {
            var response = Service.Get<IUiService>().GetCustomFormById(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }

            return View(response.Data);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Service.Get<IUiService>().DeleteFormDetail(id);

            if (Session[Constant.FormId] == null)
                return RedirectToRoute("Default", new { controller = "CustomForm" });

            int formId = (int)Session[Constant.FormId];
            return RedirectToAction("Index", new { formId = formId });
        }

        private void PopulateUiControls(FormDesign formDesign)
        {
            var responseDataSourceList = Service.Get<IUiService>().GetDataSourceList();
            var responseControlList = Service.Get<IUiService>().GetControlList();

            ViewBag.DataSourceID = new SelectList(responseDataSourceList.Data, "ID", "Name", formDesign.DataSourceID);
            ViewBag.ControlType = new SelectList(responseControlList.Data, "Name", "Name", formDesign.ControlType);
        }
    }
}